HarmonyOS-鸿蒙app开发 —基于java网络与连接_传统蓝牙远端设备操作

HarmonyOS-鸿蒙app开发 —基于java网络与连接_传统蓝牙远端设备操作

场景介绍

传统蓝牙远端管理操作主要是针对远端蓝牙设备的基本操作,包括获取远端蓝牙设备地址、类型、名称和配对状态,以及向远端设备发起配对。

接口说明

表1 蓝牙远端设备管理类BluetoothRemoteDevice的主要接口

接口名

功能描述

getDeviceAddr​()

获取远端蓝牙设备地址。

getDeviceClass​()

获取远端蓝牙设备类型。

getDeviceName​()

获取远端蓝牙设备名称。

getPairState​()

获取远端设备配对状态。

startPair​()

向远端设备发起配对。

开发步骤

  1. 调用BluetoothHost的getDefaultHost​(Context context)接口,获取BluetoothHost实例,管理本机蓝牙操作。
  2. 调用enable​Bt()接口,打开蓝牙。
  3. 调用startBtDiscovery​(),扫描设备。
  4. 调用startPair(),发起配对。

调用getDeviceAddr(),获取远端蓝牙设备地址。

// 获取蓝牙本机管理对象

BluetoothHost bluetoothHost = BluetoothHost.getDefaultHost(context);

// 调用打开接口

bluetoothHost.enableBt();

// 调用扫描接口

bluetoothHost.startBtDiscovery();

// 设置界面会显示出扫描结果列表,点击蓝牙设备去配对

BluetoothRemoteDevice device = bluetoothHost.getRemoteDev(TEST_ADDRESS);

device.startPair();

// 调用接口获取远端蓝牙设备地址

String deviceAddr = device.getDeviceAddr();

0 0 投票数
文章评分
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x